from google.protobuf.json_format import ParseDict

import coin.strategy.executor.common.parse_config as common_parse_config
from coin.proto.coin_executor_pb2 import AggressiveExecutorConfig


def check_executor_config(config: AggressiveExecutorConfig):
  assert type(config) == AggressiveExecutorConfig, type(config)

  assert config.HasField('symbol')

  common_parse_config.check_pos_config(config)
  common_parse_config.check_executor_config(config)


def check_spot_executor_config(config: AggressiveExecutorConfig):
  assert type(config) == AggressiveExecutorConfig, type(config)

  check_executor_config(config)
  assert config.min_pos >= 0


def convert_executor_config_from_dict(symbol, d_config):
  if d_config['lot_size'] == 0:
    return None

  d_config['symbol'] = symbol
  proto_config = AggressiveExecutorConfig()
  ParseDict(d_config, proto_config, ignore_unknown_fields=True)

  common_parse_config.convert_executor_config_from_dict(proto_config, d_config)

  check_executor_config(proto_config)

  return proto_config
